package com.finch.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author Levi Wang
 * @create 2021-07-21 20:07
 *
 * 可以理解为跟CountDownLatch相反的例子； 等人来齐了，再开会
 *
 *
 *
 */

public class CyclicBarrierDm {

    public static void main(String[] args) {

        CyclicBarrier cyclicBarrier = new CyclicBarrier(6, () -> {
            System.out.println("*****开会！！！");
        });

        for (int i = 1; i <= 7; i++) {

            final int tempInt = i;
            new Thread( () -> {
                System.out.println(Thread.currentThread().getName() + "\t已经来了"+tempInt+"个人。");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();

        }


    }

}
